********************************************************************************
* LOAD MAIN HOUSEHOLD FILE
********************************************************************************

u $data/households_9319_PE_r.dta, replace
tsset lnr year

********************************************************************************
* SAMPLE RESTRICTIONS
********************************************************************************

destring municipality, force replace


********************************************************************************
* SAVE IN NEW FOLDER
********************************************************************************
drop if noise_hhid == .

* get in parent death event
merge 1:1 lnr year using $data/parent_death, keep(match master) nogen

* get in inheritance event
merge 1:1 lnr year using $data/inheritance_rec, keep(match master) nogen keepusing(beq_received inheritance)
rename inheritance inheritance_rec
merge 1:1 lnr year using $data/inheritance_giv, keep(match master) nogen keepusing(beq_given inheritance)
rename inheritance inheritance_giv

* make real
replace inheritance_giv = inheritance_giv / cpi
replace inheritance_rec = inheritance_rec / cpi

* own death
merge 1:1 lnr year using $data/lnr_death, nogen

tsset lnr year
* replace hhid for the dead
replace hhid = l.hhid if hhid == . 
replace noise_hhid = l.noise_hhid if noise_hhid == .

tsset lnr year
replace wealth_a = housing_wealth_a + deposits_a + out_claims_a + bonds_a + carboat_a + stocks_a + mfund_a - debt_a + stocks_nonreg_a 
drop if wealth_a == 0 | l.wealth_a == 0


********************************************************************************
* DEFINE VARIABLES
********************************************************************************

gen housing_a = housing_wealth_a

rename out_claims_a net_private_debt_a

* flows (note nt*PB variables are already defined)
gen nt_debt_a = (debt_a*cpi - l.debt_a*l.cpi + cap_exp_interest_a*cpi)/cpi
gen nt_deposits_a = (deposits_a*cpi - l.deposits_a*l.cpi - cap_inc_interest_a*cpi)/cpi
gen nt_stocks_a = (stocks_a*cpi - l.stocks_a*l.cpi - (uncapgains_stocks_a)*cpi)/cpi
gen nt_mfund_a = (mfund_a*cpi - l.mfund_a*l.cpi - (uncapgains_mfund_a)*cpi)/cpi
gen nt_bonds_a = (bonds_a*cpi - l.bonds_a*l.cpi)/cpi
gen nt_net_private_debt_a = (net_private_debt_a*cpi - l.net_private_debt_a*l.cpi)/cpi
gen nt_stocks_nonreg_a = 0 


rename book_net_sddh_PB_a equity_PB_a
foreach var in stocks_PB housing_PB equity_PB stocks mfund {
	replace nt_`var'_a = - l.`var'_a if death == 1 & nt_`var'_a == .
}

* for interest rate schedule excercise
gen LTV_a = debt_a / housing_a

*winsorize LTV_a
replace LTV_a = 0 if LTV_a == .
replace LTV_a = 1 if LTV_a > 1

gen LTV_debt_a = debt_a*LTV_a

********************************************************************************
* SAVE
********************************************************************************

save $data/welfare_9319.dta, replace
keep if noise_hhid < 0.05
save $data/welfare_9319_m.dta, replace
keep if noise_hhid < 0.001
save $data/welfare_9319_s.dta, replace








